package iscript.uiFramework.components.form 
{
	import flash.display.Bitmap;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import iscript.uiFramework.components.panel.ScrollPanel;
	import iscript.uiFramework.components.silder.SilderPolicy;
	import iscript.uiFramework.components.UIComponent;
	import iscript.uiFramework.data.ComboBoxModel;
	import iscript.uiFramework.event.ComboBoxEvent;
	import iscript.uiFramework.UIFramework;
	import iscript.uiFramework.util.Scale9GridBitmap;
	
	internal class ComboBoxPanel extends  UIComponent
	{
		private var _panel:Scale9GridBitmap;
		private var _comboBox:ComboBox;
		private var _cellRenderItem:Vector.<CBBCellRender>;
		private var _storeData:Vector.<ComboBoxModel>;
		private var _boxScroll:ScrollPanel;
 
		
		public function ComboBoxPanel(comboBox:ComboBox,data:Vector.<ComboBoxModel>) 
		{
			_storeData = data;
			_comboBox = comboBox;
			_cellRenderItem = new Vector.<CBBCellRender>();
		}
		
		override protected function initialize():void 
		{
			
			_panel = new Scale9GridBitmap(
									UIFramework.resource.getBitmapData("GameLite.UIComponent." + this.theme + ".ComboboxPanel"),
									UIFramework.resource.getElementRectangle ("ComboBox", "ComboBoxPanel")
									);
			addChild(_panel);
			
			_boxScroll = new ScrollPanel();
			_boxScroll.x = 2;
			_boxScroll.y = 2;
			_boxScroll.HSilderPolicy = SilderPolicy.OFF;
			_boxScroll.render(this);
			
			
			
			super.initialize();
			UIFramework.UiStage.addEventListener(MouseEvent.MOUSE_DOWN, _mouseDownLogic);
		}
		
		private function _mouseDownLogic(e:MouseEvent):void 
		{
			if(!hitMouse()){
				this.visible = false;
			}
		}
		
				
		override public function get width():Number 
		{
			return _panel.width; 
		}
		
		override public function get height():Number 
		{
			return _panel.height; 
		}
		
		
		override public function render(container:DisplayObjectContainer = null):void 
		{
			var maxheight:int = 0;
			var containerSprite:Sprite = new Sprite();
			for (var i:int = 0; i <_storeData.length ; i++) 
			{
				var cellRender:CBBCellRender = new CBBCellRender(_storeData[i]);
				    cellRender.width = _comboBox.width - 4;
					cellRender.height = 20
					cellRender.y = i * 20;
					cellRender.position = i;
					cellRender.addEventListener(ComboBoxEvent.CellRender_Select,_selectCellRenderLogic)
					cellRender.render(containerSprite);
					_cellRenderItem.push(cellRender);
					maxheight += cellRender.height;
			}
			_panel.width = _comboBox.width;
			_panel.height = _comboBox.rowCount*20+ 4;
			
			_boxScroll.source = containerSprite;
			_boxScroll.width = _comboBox.width - 4;
			_boxScroll.height = _comboBox.rowCount* 20;
			_boxScroll.background = false;
			super.render(container);
		}
		
		private function _selectCellRenderLogic(e:ComboBoxEvent):void 
		{
			for each ( var item:CBBCellRender  in _cellRenderItem) {
				   if (item != e.target) {
					   item.normal(); 
				   }
				}
			this.visible = false;
			_comboBox.setText(CBBCellRender(e.target).text); 
			
			
			var event:ComboBoxEvent = new ComboBoxEvent(ComboBoxEvent.CellRender_Select);
				event.selectData = e.selectData;
				event.selectIndex = e.selectIndex;
		 	_comboBox.dispatchEvent(event)
		}
		
		override public function dispose():void 
		{
			if (!Disposed) {
				  UIFramework.UiStage.removeEventListener(MouseEvent.MOUSE_DOWN, _mouseDownLogic)
				  super.dispose()
				}
			 
		}
		
		
	}

}